﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataTransfer;
using EnglishLibrary;
using Display;
using EnglishReminder.Properties;
using Global;

namespace EnglishReminder
{
    public partial class AddEditVocabulary : Form
    {
        //the object for invoking methods
        private VocabularyDT vocabularyDT = new VocabularyDT();
        private Vocabulary vocabularyItem = new Vocabulary();
        private CategoryDT categoryDT = new CategoryDT();

        public AddEditVocabulary()
        {
            InitializeComponent();
        }

        #region Events handler
        private void btnAdd_Click(object sender, EventArgs e)
        {
            //Set the form value to vocabulary
            var setResult = SetVocabularyValue();
            if (!setResult)
                return;

            var result = vocabularyDT.InsertVocabulary(vocabularyItem);

            if (result == false)
                MessageBox.Show(Resources.Result_AddVocabularyFail, "Fail", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
                MessageBox.Show(Resources.Result_AddVocabularySuccessfully, "Successful", MessageBoxButtons.OK);

            //clear text
            ClearText();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddEditVocabulary_Load(object sender, EventArgs e)
        {
        }
        #endregion

        #region private functions
        private void ClearText()
        {
            this.tbWord.Clear();
            this.tbPronunciation.Clear();
            this.tbMeaning.Clear();
            this.rtbExample.Clear();
        }
        
        private bool SetVocabularyValue()
        {
            //Set the word (cannot empty)
            var word = this.tbWord.Text.Trim();
            if (word != string.Empty)
                vocabularyItem.Word = word;
            else
            {
                Warning_WordEmpty();
                return false;
            }

            //Set the pronunciation
            var pronunciation = this.tbPronunciation.Text.Trim();
            if (pronunciation != string.Empty)
                vocabularyItem.Pronunciation = pronunciation;

            //Set the meaning
            var meaning = this.tbMeaning.Text.Trim();
            if (meaning != string.Empty)
                vocabularyItem.Meaning = meaning;

            //Set the example
            var example = this.rtbExample.Text.Trim();
            if (example != string.Empty)
                vocabularyItem.Example = example;

            //set the rateID
            vocabularyItem.RateID = this.rateUC1.SelectedValue;
            //set the created date
            vocabularyItem.CreatedDate = DateTime.Today;
            //set the modified date
            vocabularyItem.ModifiedDate = DateTime.Today;

            //set the category (unique in database)
            int cateID = this.categoryUC1.GetSelectedCateId();
            if (cateID >= 0)
                vocabularyItem.CateID = cateID;
            else
            {
                DialogResult dialogResult = this.Warning_CateNotExist();
                if (dialogResult == DialogResult.OK)
                {   //Add to Others cate
                    vocabularyItem.CateID = categoryDT.GetCateByName(Constant.OTHERS_CATE).CategoryID;
                }
                    //Choose another cate
                else if (dialogResult == DialogResult.Cancel)
                    return false;
            }

            return true;
        }

        #endregion

        #region WarningMessageBox
        private DialogResult Warning_CateNotExist()
        {
            return MessageBox.Show("The category with name '" + categoryUC1.Text + "' is NOT existed.\n" +
                                   "Press OK to add this vocabulary to 'Others' category or Cancel to choose another category.",
                                   "Cate not exist",
                                   MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
        }
        private DialogResult Warning_WordEmpty()
        {
            return MessageBox.Show(Resources.Warning_WordEmpty, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        #endregion
    }
}
